package org.hxyjs.service.impl;


import com.fasterxml.jackson.databind.ObjectMapper;
import org.hxyjs.dao.BooksDao;
import org.hxyjs.dao.RedisDao;
import org.hxyjs.dto.DTO;
import org.hxyjs.dto.MessageEnum;
import org.hxyjs.entity.Books;
import org.hxyjs.page.PageInfo;
import org.hxyjs.service.BooksService;
import org.hxyjs.utils.GenNumTools;
import org.hxyjs.utils.JacksonUtils;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

@Service("BooksService")
public class BooksServiceImpl implements BooksService {
    @Resource
    BooksDao booksDao;
    @Resource
    RedisDao redisDao;
    @Override
    public DTO<Books> getAllBookss()  throws Exception{
        List<Books> bookss = booksDao.getAllBookss();
        MessageEnum messageEnum  = Optional.ofNullable(bookss).isPresent()?MessageEnum.GETBOOKSSUCCESS: MessageEnum.GETBOOKSFAIL;
        return new DTO<Books>(messageEnum.getCode(),messageEnum.getMessage(),bookss);
    }

    @Override
    public DTO<Books> deleteBooksByBno(String bno)  throws Exception{
        Integer result = booksDao.deleteBooksByBno(bno);
        MessageEnum messageEnum  =result> 0?MessageEnum.DELETEBOOKSSUCCESS:MessageEnum.DELETEBOOKSFAIL;
        return new DTO<Books>(messageEnum.getCode(),messageEnum.getMessage());
    }

    @Override
    public DTO<Books> updateBooksByBno(Books books)  throws Exception{
        Integer result = booksDao.updateBooksByBno(books);
        DTO<Books> dto = result > 0 ? new DTO(MessageEnum.ADDBOOKSSUCCESS.getCode(), MessageEnum.ADDBOOKSFAIL.getMessage()) :
                new DTO(MessageEnum.ADDBOOKSFAIL.getCode(), MessageEnum.ADDBOOKSFAIL.getMessage());
        ObjectMapper mapper = new ObjectMapper();
        boolean inRedisFlag = false;//默认不在redis的某一页缓存中
        for(int i=0;i<2;i++) {
            //redisDao会从redis中取出TeacherBYPage1对应的json字符串，利用自己写的JacksonUtils工具deserializeDTO反序列化出dto对象
            String jsonStr = redisDao.get("BooksByPage"+(i+1));
            System.out.println("----------------------");
            System.out.println(jsonStr);
            DTO<Books> redisBackDTO = JacksonUtils.deserializeDTO(jsonStr, DTO.class, Books.class);
            if(redisBackDTO.getCode() == 200) {//因为反序列化保存的dto对象的值一定是200，能够找到缓存状态吗就是200 而没有找到缓存的状态吗409
                for (Books booksTemp : redisBackDTO.gettList()) {
                    if (books.getBno().equals(booksTemp.getBno())) {//如果判断成立 则本次修改的数据在缓存中需要删除原来的缓存让下一次查询自动更新
                        redisDao.delete("BooksByPage" + (i + 1));
                        inRedisFlag = true;
                        break;
                    }
                }
                if (inRedisFlag) {
                    break;
                }
            }
        }
        return dto;
    }

    @Override
    public DTO<Books> addBooks(Books books) throws Exception {
//        books.setBno(GenNumTools.initId("",3,booksDao.getMaxBno()));
//        Integer result = booksDao.addBooks(books);
//        DTO<Books> dto = result > 0 ? new DTO(MessageEnum.ADDBOOKSSUCCESS.getCode(),MessageEnum.ADDBOOKSSUCCESS.getMessage()):
//                new DTO(MessageEnum.ADDBOOKSFAIL.getCode(),MessageEnum.ADDBOOKSFAIL.getMessage());
//        return dto;
        Integer result = booksDao.addBooks(books);
        MessageEnum messageEnum  = result> 0?MessageEnum.ADDBOOKSSUCCESS:MessageEnum.ADDBOOKSFAIL;
        return new DTO<Books>(messageEnum.getCode(),messageEnum.getMessage());
    }

    @Override
    public DTO<Books> getBooksByPage(PageInfo pageInfo ) throws Exception {
        List<Books> bookss = booksDao.getBooksByPage(pageInfo);
        MessageEnum messageEnum  = Optional.ofNullable(bookss).isPresent()?MessageEnum.GETBOOKSSUCCESS: MessageEnum.GETBOOKSFAIL;
        DTO<Books> dto = new DTO(messageEnum.getCode(),messageEnum.getMessage(),bookss,pageInfo);
        return dto;
    }

    @Cacheable(value = "myCache",key = "#cacheId")
    @Override
    public DTO<Books> getBooksByPage(PageInfo pageInfo, String cacheId) throws Exception {
        List<Books> bookss = booksDao.getBooksByPage(pageInfo);
        MessageEnum messageEnum = bookss.size() > 0?MessageEnum.ADDBOOKSSUCCESS: MessageEnum.ADDBOOKSFAIL;
        DTO<Books> dto = new DTO(messageEnum.getCode(),messageEnum.getMessage(),bookss,pageInfo);
        return dto;
    }
}
